CREATE FUNCTION ReportingPeriodMonths (@id INT)
RETURNS NVARCHAR(MAX)
WITH EXECUTE AS CALLER
AS
BEGIN
	-- RETURNS a comma-delimited list of year-months given a ReportingPeriod Id.
	-- Example of results:
	-- 201501, 201502, 201503, 201504
    DECLARE @Names NVARCHAR(MAX);

    SELECT  @Names = COALESCE(@Names + ', ', '') + dc.ClaimMonth
    FROM    (
            SELECT DISTINCT SUBSTRING(CONVERT(VARCHAR, c.ClaimDate, 112), 1, 6) 'ClaimMonth'
            FROM Claim c
            WHERE c.ReportingPeriodId = @id
            ) dc;

     RETURN(@Names);
END;
